#######################################
#### PARALLEL TRENDS: FIGURE A1 #######
#######################################

#### LOAD PACKAGES ####

library(haven)
library(dplyr)
library(tidyr)
library(ggpubr)

##### Set working directory - to the JOP Replication files folder ###
#### Ignore path in line 14 and replace with path in your own computer

#setwd("/Users/rithika/Dropbox/Dissertation/JOP Replication files/")

# 1. Fertility: Left Panel  - Import data 
birth_hist <- read_dta("DATA FILES TO SHARE/TEMP_FILES/birth_hist.dta")

## Collapse data to get the fertility rate by year for treatment and control groups
df <- birth_hist %>%
  group_by(pre_trt_yr,w2_abshusband_dummy) %>%
  summarize(number.children = n())  %>%
  mutate(fertility = ifelse(w2_abshusband_dummy == 0,
                            number.children/21208,number.children/818) )


df$w2_abshusband_dummy <- as.factor(df$w2_abshusband_dummy)
levels(df$w2_abshusband_dummy) <- c("Co-resident","Migrant in Wave 2")
colnames(df)[2] <- "Husband_Status"

library(ggplot2)
p<-ggplot(df, aes(x=pre_trt_yr, y=fertility, group=Husband_Status)) +
  geom_line(aes(linetype=Husband_Status))+
  geom_point(aes()) +
  xlim(c(-15,-1))+ 
  labs(title="Fertility",x="No. of years pre-treatment", y = "Avg. no of children birthed per woman in a given year")+
  theme_bw()
p
### rows with births prior to t-15 years will be dropped 


### 2. Loans: Right Panel - Import data  
loans_2012 <- read_dta("/Users/rithika/Dropbox/Dissertation/JOP Replication files/DATA FILES TO SHARE/TEMP_FILES/loans_12.dta")
loans_2012$w2_abshusband_dummy <- as.factor(loans_2012$w2_abshusband_dummy)
loans_2012$loan_yr <- 2012-loans_2012$DB2A
loans_2012$pre_trt_yr <- loans_2012$loan_yr-2012

## Collapse data to get laons by treamtent and control 
loans <- loans_2012%>%
  group_by(pre_trt_yr,w2_abshusband_dummy) %>%
  summarize(loan_count = n())  %>%
  mutate(loan.rate = ifelse(w2_abshusband_dummy == 0,
                            loan_count/13747 ,loan_count/630) )

levels(loans$w2_abshusband_dummy) <- c("Co-resident","Migrant in Wave 2")
colnames(loans)[2] <- "Husband_Status"

## Make plot 
p3<-ggplot(loans, aes(x=pre_trt_yr, y=loan.rate, group=Husband_Status)) +
  geom_line(aes( linetype=Husband_Status))+
  geom_point(aes())+
  theme_bw() + xlim(c(-15,-1))+ 
  labs(title="Loans",x="No. of years pre-treatment", y = "Avg. no of loans taken in a given year")
p3

ggarrange(p, p3, ncol=2, nrow=1, common.legend = TRUE, legend="bottom")
ggsave("OUTPUT/GRAPHS/pre_trends.png")


